﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace TS_XY_Train_Repaire.ToolSystem
{
    public partial class Tool_System_ToolEdit_Form : DevExpress.XtraEditors.XtraForm
    {
        // 工具id，如果为0则为添加工具
        public TS_Tool m_oTool = new TS_Tool();
        private CRfid m_rfid = new CRfid();
        private System.Windows.Forms.Timer m_tTimer = new System.Windows.Forms.Timer();//实例化Timer类，设置间隔时间为10000毫秒；

        public Tool_System_ToolEdit_Form()
        {
            InitializeComponent();
        }

        private void Tool_System_ToolEdit_Form_Load(object sender, EventArgs e)
        {
            cb_toolpos.DataSource = ToolDataMgr.GetInstance().GetLeafToolPos();
            cb_toolpos.ValueMember = "f_id";
            cb_toolpos.DisplayMember = "f_name";

            cb_tooltype.DataSource = ToolDataMgr.GetInstance().GetLeafToolType();
            cb_tooltype.ValueMember = "f_id";
            cb_tooltype.DisplayMember = "f_name";

            if (m_oTool.IID < 0)
            {
                bt_Add.Enabled = true;
                bt_Modify.Enabled = false;

                de_fcDate.DateTime = m_oTool.OFcDate;

                cb_box.SelectedIndex = 0;
                cb_status.SelectedIndex = 0;
                cb_status.SelectedIndex = 0;
                cb_box.SelectedIndex = 0;
            }
            else
            {
                bt_Add.Enabled = false;
                bt_Modify.Enabled = true;
                cb_box.Enabled = false;

                cb_toolpos.SelectedValue = m_oTool.IPosID;
                cb_tooltype.SelectedValue = m_oTool.ITypeID;

                te_RfID.Text = m_oTool.SRfID;
                te_FcID.Text = m_oTool.SFactoryID;
                te_FcPrice.Text = m_oTool.FPrice.ToString();
                de_fcDate.DateTime = m_oTool.OFcDate;
                te_FcCode.Text = m_oTool.SFcCode;

                //rg_Status
                cb_box.SelectedIndex = m_oTool.IToolBox;
                cb_status.SelectedIndex = m_oTool.IStatusType;
            }

            if (m_rfid.Init())
            {
                // 开启定时器
                m_tTimer.Interval = 1000;
                m_tTimer.Tick += new EventHandler(timer_Tick);
                m_tTimer.Enabled = true;
            }
            else
            {
                MessageBox.Show("打开rfid串口失败!");
            }
        }

        private void bt_Add_Click(object sender, EventArgs e)
        {
            if (!CheckValid())
                return;

            SaveData();

            if (ToolDataMgr.GetInstance().AddTool(m_oTool))
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void bt_Modify_Click(object sender, EventArgs e)
        {
            lock(this)
            {
                if (!CheckValid())
                    return;

                m_tTimer.Stop();

                SaveData();

                if (ToolDataMgr.GetInstance().ModifyTool(m_oTool))
                {
                    this.DialogResult = DialogResult.OK;
                }
            }
        }

        private void bt_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private bool CheckValid()
        {
            // 检查是否都有赋值
            if (cb_box.SelectedIndex < 0)
            {
                MessageBox.Show("请选择工具箱类型", "提示", MessageBoxButtons.OK);
                return false;
            }

            if (cb_status.SelectedIndex < 0)
            {
                MessageBox.Show("请选择工具状态", "提示", MessageBoxButtons.OK);
                return false;
            }

            if (cb_toolpos.SelectedIndex < 0)
            {
                MessageBox.Show("请选择工具位置", "提示", MessageBoxButtons.OK);
                return false;
            }

            if (cb_tooltype.SelectedIndex < 0)
            {
                MessageBox.Show("请选择工具类型", "提示", MessageBoxButtons.OK);
                return false;
            }

            if (te_RfID.Text.Length > 0 && ToolDataMgr.GetInstance().IsRFIDExist(te_RfID.Text))
            {
                MessageBox.Show("当前RDID已存在", "提示", MessageBoxButtons.OK);
                return false;
            }

            return true;
        }

        private void SaveData()
        {
            m_oTool.ITypeID = (int)cb_tooltype.SelectedValue;
            m_oTool.SRfID = te_RfID.Text;
            m_oTool.IPosID = (int)cb_toolpos.SelectedValue;
            m_oTool.SFactoryID = te_FcID.Text;
            m_oTool.OFcDate = de_fcDate.DateTime;
            m_oTool.SFcCode = te_FcCode.Text;

            m_oTool.IStatusType = cb_status.SelectedIndex;
            m_oTool.IToolBox = cb_box.SelectedIndex;

            m_oTool.FPrice = te_FcPrice.Text.Length == 0 ? 0 : double.Parse(te_FcPrice.Text);
        }

        void timer_Tick(object sender, EventArgs e)
        {
            lock(this)
            {
                List<string> listRFID = new List<string>();
                if (!m_rfid.GetRFID(ref listRFID))
                    return;

                if (listRFID.Count > 0)
                {
                    string strRFID = listRFID[0];
                    if (strRFID != te_RfID.Text)
                    {
                        te_RfID.Text = strRFID;
                    }
                }
            }
        }

        private void Tool_System_ToolEdit_Form_FormClosing(object sender, FormClosingEventArgs e)
        {
            m_rfid.UnInit();
            m_tTimer.Enabled = false;
        }
    }
}